﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.IO;
using System.Drawing;
using Reviewer.Utilities.Drawing;
using System.Drawing.Imaging;

public partial class CMSV2_Controls_GameTopImage : System.Web.UI.UserControl
{
    public Guid GameId
    {
        get;
        set;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Guid.Empty.Equals(GameId))
            {
                this.Visible = false;
            }
            else
            {
                BindTopImage();
            }
        }
    }

    private void BindTopImage()
    {
        using (IGameService service = ServiceFactory.GameService)
        {
            GameTopImage image = service.GetAssociatedTopImage(GameId);
            if (image != null)
            {
                string filename = ResolveUrl(String.Format("~/SpilTop/{0}", image.FileName));
                if (File.Exists(MapPath(filename)))
                {
                    this.uiImgTopImage.ImageUrl = filename + "?uid=" + Guid.NewGuid().ToString(); ;
                }

                if (image.LogoInText)
                {
                    this.uiRblTopImageTitle.SelectedValue = "0";
                }
                else if (image.LogoTextColor.ToUpperInvariant().Equals("#000000", StringComparison.Ordinal))
                {
                    this.uiRblTopImageTitle.SelectedValue = "2";
                }
                else
                {
                    this.uiRblTopImageTitle.SelectedValue = "1";
                }
            }
        }
    }
    protected void uiBtnChangeTopImage_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            using (IGameAdminService adminService = ServiceFactory.GameAdminService)
            using (IGameService service = ServiceFactory.GameService)
            {
                GameTopImage image = service.GetAssociatedTopImage(GameId);
                if (image == null)
                {
                    image = new GameTopImage();
                    image.GameId = this.GameId;
                }
                switch (this.uiRblTopImageTitle.SelectedValue)
                {
                    case "0":
                        image.LogoInText = true;
                        image.LogoTextColor = "#000000";
                        break;
                    case "1":
                        image.LogoInText = false;
                        image.LogoTextColor = "#FFFFFF";
                        break;
                    case "2":
                        image.LogoInText = false;
                        image.LogoTextColor = "#000000";
                        break;
                }

                adminService.SetAssociatedTopImage(image);

                uiImgTopImage.ImageUrl = ResolveClientUrl(ResolveUrl("~/SpilTop/" + image.FileName)) + "?uid=" + Guid.NewGuid().ToString();
            }
        }
    }

    public void uiImageUploader_OnImageTransformationComplte(object sender, System.Drawing.Image transformedImage)
    {
        using (IGameAdminService adminService = ServiceFactory.GameAdminService)
        using (IGameService service = ServiceFactory.GameService)
        {
            GameTopImage image = service.GetAssociatedTopImage(GameId);
            if (image == null)
            {
                image = new GameTopImage();
                image.GameId = this.GameId;
                image.LogoInText = true;
                image.LogoTextColor = "#000000";
            }

            if (String.IsNullOrEmpty(image.FileName) || !image.FileName.ToUpperInvariant().EndsWith(".PNG", StringComparison.Ordinal))
            {
                image.FileName = String.Format("{0}.png", image.GameId.ToString());
            }

            transformedImage.Save(MapPath(ResolveUrl("~/SpilTop/" + image.FileName)), ImageFormat.Png);

            adminService.SetAssociatedTopImage(image);
            this.uiImgTopImage.ImageUrl = ResolveClientUrl(ResolveUrl("~/SpilTop/" + image.FileName)) + "?uid=" + Guid.NewGuid().ToString();
        }

    }
}
